package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimail.product.dao.AttrDao;
import com.atguigu.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimail.product.service.*;
import com.atguigu.gulimail.product.vo.Attr;
import com.atguigu.gulimail.product.vo.AttrGroupAndAttrsVo;
import com.atguigu.gulimail.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimail.product.vo.AttrResponVo;
import com.atguigu.gulimail.product.web.Vo.SkuItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.AttrGroupDao;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrAttrgroupRelationDao relationDao;

    @Autowired
    AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catLogId) {

        String key=(String)params.get("key");

        if(catLogId==0){

            LambdaQueryWrapper<AttrGroupEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            if(!key.isEmpty()){

                lambdaQueryWrapper.like(AttrGroupEntity::getAttrGroupName,key)
                            .or().eq(AttrGroupEntity::getAttrGroupId,key)
                            .or().like(AttrGroupEntity::getDescript,key);
            }


            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    lambdaQueryWrapper
            );
            return new PageUtils(page);

        }
        LambdaQueryWrapper<AttrGroupEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttrGroupEntity::getCatelogId,catLogId);
        if(!key.isEmpty()){
            lambdaQueryWrapper.and((wrapper)->{
                wrapper.like(AttrGroupEntity::getAttrGroupName,key)
                        .or().eq(AttrGroupEntity::getAttrGroupId,key)
                        .or().like(AttrGroupEntity::getDescript,key);
            });
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                lambdaQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getInfo(Long attrGroupId) {

        AttrGroupEntity attrGroup = this.getById(attrGroupId);

        List<Long> path =new ArrayList<>();
        Long catLogId=attrGroup.getCatelogId();
        while (catLogId!=0){
            path.add(catLogId);
            catLogId=categoryService.getById(catLogId).getParentCid();
        }
        Collections.reverse(path);//
        attrGroup.setCatePath(path);
        return attrGroup;
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                .eq("attr_group_id", attrgroupId));
        if (relationEntities!=null){
            List<AttrEntity> attrEntityList=new ArrayList<>();
            for (AttrAttrgroupRelationEntity item:relationEntities) {
                Long attrId=item.getAttrId();
                AttrEntity attr = attrDao.selectById(attrId);
                attrEntityList.add(attr);
            }
            return attrEntityList;

        }
        return null;
    }

    @Override
    public void deleteRelations(List<AttrAttrgroupRelationEntity> collect) {
        relationDao.deleteReationList(collect);
    }

    @Override
    public List<AttrGroupAndAttrsVo> getAttrGroupAndAttrs(Long catelogId) {

        List<AttrGroupEntity> attrGroups = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));

        if(!attrGroups.isEmpty()) {
            List<AttrGroupAndAttrsVo> vos = attrGroups.stream().map((item) -> {
                AttrGroupAndAttrsVo attrGroupAndAttrsVo = new AttrGroupAndAttrsVo();
                BeanUtils.copyProperties(item, attrGroupAndAttrsVo);
                List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item.getAttrGroupId()));

                if (!relationEntities.isEmpty()) {
                    List<Long> attrIds = relationEntities.stream().map((i) -> {
                        return i.getAttrId();
                    }).collect(Collectors.toList());

                    List<AttrEntity> attrEntityList = attrDao.selectList(new QueryWrapper<AttrEntity>().in("attr_id", attrIds));
                    attrGroupAndAttrsVo.setAttrs(attrEntityList);
                }

                return attrGroupAndAttrsVo;
            }).collect(Collectors.toList());
            return vos;
        }
        return null;
    }

    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    AttrAttrgroupRelationService relationService;
    @Autowired
    AttrService attrService;
    @Autowired
    SkuSaleAttrValueService saleAttrValueService;
    @Autowired
    ProductAttrValueService productAttrValueService;
    @Override
    public List<SkuItemVo.SpuItemAttrGroupVo> getAttrGroupAndAttrsBySpuId(Long spuId,Long catelogId) {
        //查出当前spu对应的所有属性的分组信息,以及当前分组下的所有属性对应的值

            //首先根据catalogid查出属性分组
        LambdaQueryWrapper<AttrGroupEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
        List<AttrGroupEntity> attrGroupList = attrGroupService.list(wrapper);

        if(attrGroupList!=null){

            //拿到商品对应的属性分组的Ids
            List<Long> groupIds=attrGroupList.stream().map(item-> item.getAttrGroupId())
                    .collect(Collectors.toList());

            LambdaQueryWrapper<AttrAttrgroupRelationEntity> relationWrapper=new LambdaQueryWrapper<>();
            relationWrapper.in(AttrAttrgroupRelationEntity::getAttrGroupId,groupIds);
            List<AttrAttrgroupRelationEntity> relationEntities = relationService.list(relationWrapper);
            if (relationEntities!=null){
                List<Long> attrIds = relationEntities.stream().map(i -> i.getAttrId())
                        .collect(Collectors.toList());

                LambdaQueryWrapper<ProductAttrValueEntity> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.in(ProductAttrValueEntity::getAttrId,attrIds).eq(ProductAttrValueEntity::getSpuId,spuId);

                List<ProductAttrValueEntity> valueList = productAttrValueService.list(queryWrapper);

                List<SkuItemVo.SpuItemAttrGroupVo> spuItemAttrGroupVos;

                spuItemAttrGroupVos=attrGroupList.stream().map(group->{
                    SkuItemVo.SpuItemAttrGroupVo spuItemAttrGroupVo = new SkuItemVo.SpuItemAttrGroupVo();
                    spuItemAttrGroupVo.setGroupName(group.getAttrGroupName());

                    List<Attr> attrs;

                    attrs = valueList.stream().map(value -> {
                        if (attrIds.contains(value.getAttrId())) {
                            Attr attr = new Attr();
                            attr.setAttrValue(value.getAttrValue());
                            attr.setAttrId(value.getAttrId());
                            attr.setAttrName(value.getAttrName());
                            return attr;
                        }
                        return null;
                    }).collect(Collectors.toList());

                    if (attrs!=null){
                        spuItemAttrGroupVo.setAttrs(attrs);
                    }
                    return spuItemAttrGroupVo;
                }).collect(Collectors.toList());

                return spuItemAttrGroupVos;
            }


        }
        return null;
    }


}